package cn.ddiancan.xddcloud.ddccore.mulidatasource;

import cn.ddiancan.xddcloud.ddccore.mulidatasource.context.DataSourceContextHolder;
import cn.ddiancan.xddcloud.ddccore.mulidatasource.entity.DataSourceEnum;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * 动态/多数据源
 *
 * @author Amos
 * @since 2024-08-19
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    // 返回所需数据源标识
    @Override
    protected Object determineCurrentLookupKey() {
        String dataSourceKey = DataSourceContextHolder.getDataSourceKey();
        if (dataSourceKey == null) {
            dataSourceKey = String.valueOf(DataSourceEnum.Master);
        }
        return dataSourceKey;
    }
}
